package com.training.bt.controller;

import com.training.bt.entity.Promotion;
import com.training.bt.entity.User;
import com.training.bt.security.util.SecurityUtils;
import com.training.bt.service.IPromotionService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import lombok.RequiredArgsConstructor;

import java.util.List;

@RestController
@RequestMapping("/api/promotions")
@RequiredArgsConstructor
public class PromotionController {

    private final IPromotionService promotionService;

    // 创建促销活动
    @PostMapping
    public ResponseEntity<Promotion> createPromotion(@RequestBody Promotion promotion) {
        if (promotion == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        User user = SecurityUtils.getCurrentUser();
        promotion.setCreatorId(user.getId());
        Promotion createdPromotion = promotionService.createPromotion(promotion);
        return new ResponseEntity<>(createdPromotion, HttpStatus.CREATED);
    }

    // 获取单个促销活动
    @GetMapping("/{promotionId}")
    public ResponseEntity<Promotion> getPromotionById(@PathVariable Long promotionId) {
        Promotion promotion = promotionService.getPromotionById(promotionId);
        if (promotion != null) {
            return new ResponseEntity<>(promotion, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 获取所有促销活动
    @GetMapping
    public ResponseEntity<List<Promotion>> getAllPromotions() {
        List<Promotion> promotions = promotionService.getAllPromotions();
        return new ResponseEntity<>(promotions, HttpStatus.OK);
    }

    // 获取所有生效的促销活动
    @GetMapping("/active")
    public ResponseEntity<List<Promotion>> getActivePromotions() {
        List<Promotion> promotions = promotionService.getActivePromotions();
        return new ResponseEntity<>(promotions, HttpStatus.OK);
    }

    // 获取正在进行的促销活动
    @GetMapping("/in-progress")
    public ResponseEntity<List<Promotion>> getInProgressPromotions() {
        List<Promotion> promotions = promotionService.getInProgressPromotions();
        return new ResponseEntity<>(promotions, HttpStatus.OK);
    }

    // 更新促销活动（全量更新）
    @PutMapping
    public ResponseEntity<Promotion> updatePromotion(@RequestBody Promotion promotion) {
        if (promotion == null || promotion.getPromotionId() == null) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        Promotion updatedPromotion = promotionService.updatePromotion(promotion);
        return new ResponseEntity<>(updatedPromotion, HttpStatus.OK);
    }

    // 修改促销活动状态
    @PatchMapping("/{promotionId}/status")
    public ResponseEntity<Promotion> updatePromotionStatus(
        @PathVariable Long promotionId,
        @RequestParam Boolean isActive
    ) {
        Promotion updatedPromotion = promotionService.updatePromotionStatus(promotionId, isActive);
        if (updatedPromotion != null) {
            return new ResponseEntity<>(updatedPromotion, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 删除促销活动
    @DeleteMapping("/{promotionId}")
    public ResponseEntity<Void> deletePromotion(@PathVariable Long promotionId) {
        promotionService.deletePromotion(promotionId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}